package com.wyb.handler;


import com.alibaba.fastjson.JSON;
import com.wyb.constant.Result;
import com.wyb.utils.WebUtils;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {//权限检验异常处理器
    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
        Result result = new Result(HttpStatus.FORBIDDEN.value(), "拒绝访问");
        String json = JSON.toJSONString(result);
        System.out.println(json);
        WebUtils.renderString(httpServletResponse, json);
    }
}
